/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.course;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.config.Global;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduSection;
import com.idenger.modules.edu.service.course.EduCourseContentService;
import com.idenger.modules.edu.service.practice.EduPracticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.utils.StringUtils;
import com.idenger.modules.edu.entity.course.CourseVideoAndPracticeDto;
import com.idenger.modules.edu.service.course.EduSectionService;

/**
 * 章节Controller
 * @author chenlin
 * @version 2017-12-01
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/course/eduSection")
public class EduSectionController extends BaseController {

	@Autowired
	private EduSectionService eduSectionService;
	
	@Autowired
	private EduCourseContentService eduCourseContentService;
	
	@Autowired
	private EduPracticeService eduPracticeService;
	
	@ModelAttribute
	public EduSection get(@RequestParam(required=false) String id) {
		EduSection entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduSectionService.get(id);
		}
		if (entity == null){
			entity = new EduSection();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:course:eduSection:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduSection eduSection, HttpServletRequest request, HttpServletResponse response, Model model) {
		//根绝课程ID查询出课程下所有的章节(单元)
		List<EduSection> eduSectionList = eduSectionService.findEduSectionByCourseId(eduSection.getCourseId());
		for (EduSection educontentSetion : eduSectionList) {
			//查询出单元下的所有的视频
			//视频列表
			List<CourseVideoAndPracticeDto>	eduCourseContentList=eduCourseContentService.findEduCourseContentBysectionId(educontentSetion.getId());
			//练习列表
			List<CourseVideoAndPracticeDto> eduCourseContentList2 = eduPracticeService.findEduPracticeBysectionId(educontentSetion.getId());
			educontentSetion.setCourseVideoDtoList(eduCourseContentList);
			educontentSetion.setCoursePracticeDtoList(eduCourseContentList2);
		} 
		model.addAttribute("eduSectionList", eduSectionList);
		model.addAttribute("courseId", eduSection.getCourseId());
		return "modules/edu/course/eduSectionList";
	}

	@RequiresPermissions("edu:course:eduSection:view")
	@RequestMapping(value = "form")
	public String form(EduSection eduSection, Model model,HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("eduSection", eduSection);
		return "modules/edu/course/eduSectionForm";
	}

	@RequiresPermissions("edu:course:eduSection:edit")
	@RequestMapping(value = "save")
	public String save(EduSection eduSection, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
		if (!beanValidator(model, eduSection)){
			return form(eduSection, model,request,response);
		}
		System.out.println("添加");
		eduSectionService.save(eduSection);
		addMessage(redirectAttributes, "保存章节成功");
		
		return "redirect:"+ Global.getAdminPath()+"/edu/course/eduSection/?repage&courseId="+ eduSection.getCourseId();
	}
	
	
	@RequiresPermissions("edu:course:eduSection:edit")
	@RequestMapping(value = "save2")
	public String save2(EduSection eduSection, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
		BaseResponseMsg baseResponseMsg;
		eduSectionService.save(eduSection);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(eduSection);
		return renderString(response, baseResponseMsg);
	}




	@RequiresPermissions("edu:course:eduSection:edit")
	@RequestMapping(value = "delete2")
	public String delete2(EduSection eduSection,HttpServletResponse response) {
		BaseResponseMsg baseResponseMsg;
		eduSectionService.delete(eduSection);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(eduSection);
		return renderString(response, baseResponseMsg);
	}
	
	
	@RequiresPermissions("edu:course:eduSection:edit")
	@RequestMapping(value = "delete")
	public String delete(EduSection eduSection, RedirectAttributes redirectAttributes) {
		eduSectionService.delete(eduSection);
		addMessage(redirectAttributes, "删除章节成功");
		return "redirect:"+Global.getAdminPath()+"/edu/course/eduSection/?repage";
	}

}